from DrissionPage import ChromiumPage
from util import dp_helper, cv2_util, file_helper, my_tkinter
from pojo.Product import Product_1688


class pc1688_class:
    def __init__(self, page_xz_下载: dp_helper.xz_下载_class) -> None:
        page = ChromiumPage()
        page.get("https://www.1688.com/")
        self.page = page
        self.page_xz_下载 = page_xz_下载

    def close(self):
        self.page.close()

    def sstp_搜索图片(self, img_path, tzdsl_特征点数量: int):
        def sctp_上传图片(page: ChromiumPage):
            a = page.eles("以图搜款")
            a[1].click.to_upload(img_path)
            page.wait.doc_loaded()

        while True:
            print(f"23")
            tab = dp_helper.hqxbqy_网页操作_获取新标签页(
                self.page, sctp_上传图片, "批发_供应_阿里巴巴", time=9
            )
            if tab == None:
                my_tkinter.文本框("没有找到1688标签页，准备重试")
                tab.close()
            else:
                break
        es = tab.eles("x://div[@class='space-offer-card-box']")
        for e in es:
            e_a = e.ele("x://div[@class='img-container']//a")
            url_e = e_a.attr("href")
            e_img = e.ele("x://div[@class='img']")
            img_url = e_img.attr("style")
            img_url = img_url.replace('background-image: url("', "")
            img_url = img_url[:-3]
            folder_path = "img"
            file_qz = "1688"
            # bl = xzbpdtpsfxz_下载并判断图片是否相似(
            #     page_xz_下载=self.page_xz_下载,
            #     folder_path=folder_path,
            #     file_qz=file_qz,
            #     url_img=img_url,
            #     img_path=img_path,
            #     tzdsl_特征点数量=tzdsl_特征点数量,
            # )
            self.page_xz_下载.xzwj_下载文件(
                url=img_url, folder_path=folder_path, file_qz=file_qz
            )
            file_name = file_helper.gjwjmqztqwjm_根据文件名前缀提取文件名(
                file_qz=file_qz, folder_path=folder_path
            )
            bl = cv2_util.pdtpsfxs_判断图片是否相似(
                img1_path=img_path,
                img2_path=f"{folder_path}/{file_name}",
                tzdsl_特征点数量=tzdsl_特征点数量,
                sfksh_是否可视化=True,
            )
            if bl:
                tab.close()
                print("相似------------------------------------", url_e)
                jg_价格 = e.ele("x://div[@class='price']")
                print(jg_价格)
                # return url_e
        tab.close()
        return None


# def xzbpdtpsfxz_下载并判断图片是否相似(
#     page_xz_下载: dp_helper.xz_下载_class,
#     folder_path: str,
#     file_qz: str,
#     url_img: str,
#     img_path: str,
#     tzdsl_特征点数量: int,
# ):
#     # file_helper.scwj_删除文件(
#     #     f"{file_helper.hqgml_获取当前程序的根目录()}\\{folder_path}\\{file_name}.jpg"
#     # )
#     # file_helper.gjwjmqzscwj_根据文件名前缀删除文件(
#     #     file_qz=file_qz,
#     #     folder_path=f"{file_helper.hqgml_获取当前程序的根目录()}\\{folder_path}",
#     # )
#     page_xz_下载.xzwj_下载文件(url=url_img, folder_path=folder_path, file_name=file_qz)
#     file_name = file_helper.gjwjmqztqwjm_根据文件名前缀提取文件名(
#         file_qz=file_qz, folder_path=folder_path
#     )
#     bl = cv2_util.pdtpsfxs_判断图片是否相似(
#         img1_path=img_path,
#         img2_path=f"{folder_path}/{file_name}",
#         tzdsl_特征点数量=tzdsl_特征点数量,
#     )
#     # try:
#     bl = cv2_util.pdtpsfxs_判断图片是否相似(
#         img1_path=img_path,
#         img2_path=f"{folder_path}/{file_name}",
#         tzdsl_特征点数量=tzdsl_特征点数量,
#     )
#     return bl
# except Exception as e:
#     print("pc_爬虫  错误  41")
#     bl = False
#     return bl
